﻿

#region Using directives
using System;
using System.Web.UI.WebControls;
using InvoiceChecking.Data;
using InvoiceChecking.Entities;
using InvoiceChecking.Web.UI;
#endregion

public partial class Admin_CheckInvoice : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        FormUtil.SetPageIndex(GridView1, "page");
        if (!IsPostBack)
            BindGrid();
    }

    private void BindGrid()
    {
        string whereClause = "Received=1 AND DATEDIFF(d,ReceivedDate,GETDATE())=0";
        int count;
        VList<AllInvoice> allInvoices = DataRepository.AllInvoiceProvider.GetPaged(whereClause, "ReceivedDate DESC", 0, 10, out count);
        GridView1.DataSource = allInvoices;
        GridView1.DataBind();
    }

    protected void btn_Receive_Click(object sender, EventArgs e)
    {
        if (tb_InvoiceNumber.Text.Length == 11)
        {
            string invoiceNumber = tb_InvoiceNumber.Text.Substring(0, 10);
            DailyTrans dailyTrans = DataRepository.DailyTransProvider.GetByVatNumberCmsNumber(invoiceNumber,
                                                                                              invoiceNumber.Substring(
                                                                                                  5, 5));
            if (dailyTrans == null)
            {
                DataRepository.DailyTransProvider.CreateInvoice(invoiceNumber);
                dailyTrans = DataRepository.DailyTransProvider.GetByVatNumberCmsNumber(invoiceNumber,
                                                                                              invoiceNumber.Substring(
                                                                                                  5, 5));
            }
            if (dailyTrans.Received != true)
            {
                char version = tb_InvoiceNumber.Text[10];
                string paymentMode = dailyTrans.PmntModeRevised;
                if (string.IsNullOrEmpty(paymentMode))
                    paymentMode = dailyTrans.UpdPayAcctcode;
                AbAccountCode abAccountCode =
                    DataRepository.AbAccountCodeProvider.GetByKindPment(paymentMode);
                if (abAccountCode.CheckVersion[0] == version)
                {
                    dailyTrans.Received = true;
                    dailyTrans.ReceivedDate = DateTime.Now;
                    DataRepository.DailyTransProvider.Save(dailyTrans);
                    tb_InvoiceNumber.Text = "";
                    BindGrid();
                }
            }

        }
        tb_InvoiceNumber.Focus();
    }
    protected void btn_Print_Click(object sender, EventArgs e)
    {
        Response.Redirect("ReportCheck.aspx", true);
    }

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "UpdatePaymentMode")
        {
            string vatInvoiceNumber = e.CommandArgument.ToString().Substring(5);
            GridViewRow grvRow = (GridViewRow)((LinkButton)e.CommandSource).NamingContainer;
            EntityDropDownList ddl_RevisePaymentMode = (EntityDropDownList)grvRow.FindControl("ddl_RevisePaymentMode");
            EntityDropDownList ddl_VoidType = (EntityDropDownList)grvRow.FindControl("ddl_VoidType");
            DailyTrans dailyTrans = DataRepository.DailyTransProvider.GetByVatNumberCmsNumber(e.CommandArgument.ToString(),
                                                                                             vatInvoiceNumber);
            if (ddl_RevisePaymentMode.SelectedIndex > 0 | (ddl_VoidType.SelectedIndex > 0 | !String.IsNullOrEmpty(dailyTrans.VoidType)))
            {
                DateTime? dateVisit = Convert.ToDateTime(grvRow.Cells[6].Text);
                DataRepository.DailyTransProvider.UpdatePaymentMode(vatInvoiceNumber, dateVisit,
                                                                    ddl_RevisePaymentMode.SelectedValue, ddl_VoidType.SelectedItem.Text);
            }
            TextBox tb_Remark = (TextBox)grvRow.FindControl("tb_Remark");

            if (dailyTrans.Remark != tb_Remark.Text)
            {
                dailyTrans.Remark = tb_Remark.Text;
                dailyTrans.PmntModeRevised = ddl_RevisePaymentMode.SelectedValue;
                dailyTrans.VoidType = ddl_VoidType.SelectedItem.Text;
                DataRepository.DailyTransProvider.Update(dailyTrans);
            }
        }
        else if (e.CommandName == "RemoveInvoice")
        {
            string vatInvoiceNumber = e.CommandArgument.ToString();
            DailyTrans dailyTrans = DataRepository.DailyTransProvider.GetByVatNumberCmsNumber(vatInvoiceNumber,
                                                                                              vatInvoiceNumber.Substring(
                                                                                                  5, 5));
            dailyTrans.Received = false;
            dailyTrans.ReceivedDate = null;
            DataRepository.DailyTransProvider.Update(dailyTrans);
            BindGrid();
        }
    }
}


